package com.gaogzhen.algorithm.leetCode;

import java.util.*;

/**
 * 771. 宝石与石头
 */
public class JewelStone {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String jewels = scanner.next();
        String stones = scanner.next();
        System.out.println(numJewelsInStones1(jewels, stones));
    }



    public static int numJewelsInStones(String jewels, String stones) {
        int jewelsCount = 0;
        Set<Character> jewelsSet = new HashSet<Character>();
        int jewelsLength = jewels.length(), stonesLength = stones.length();
        for (int i = 0; i < jewelsLength; i++) {
            char jewel = jewels.charAt(i);
            jewelsSet.add(jewel);
        }
        for (int i = 0; i < stonesLength; i++) {
            char stone = stones.charAt(i);
            if (jewelsSet.contains(stone)) {
                jewelsCount++;
            }
        }
        return jewelsCount;
    }


    public static int numJewelsInStones1(String jewels, String stones) {
        int length = 0;
        char[] jewelsArray = jewels.toCharArray();
        char[] stonesArray = stones.toCharArray();
        int b = 0;
        for (int i = 0; i < jewelsArray.length; i++) {
            for (int j = 0; j < stonesArray.length-b; j++) {
                if (jewelsArray[i] == stonesArray[j]){
                    length++;
                    stonesArray[j] = stonesArray[stonesArray.length-b-1];
                    b++;
                    j -= 1;
                }
            }
        }
        return length;
    }
    public static int numJewelsInStones2(String jewels, String stones) {
        return stones.length() - (stones.replaceAll("[" + jewels + "]", "").length());
    }


}
